/* eslint-disable */
import axios from 'axios';
import { Loading } from 'element-ui';
import { MessageBox } from 'element-ui';
import VueCookie from 'vue-cookie'
import that from '../main'
axios.defaults.timeout = 5000;  //超时时间设置
axios.defaults.withCredentials = true;  //true允许跨域

if (process.env.NODE_ENV === 'production') {
  /*第二层if，根据.env文件中的VUE_APP_FLAG判断是生产环境还是测试环境*/
  if (process.env.VUE_APP_FLAG === 'pro') {
    //production 生产环境
    axios.defaults.baseURL = 'http://localhost:8888';
  } else {
    //test 测试环境
    axios.defaults.baseURL = 'http://localhost:8888';
  }
} else {
  //dev 开发环境
  axios.defaults.baseURL = 'http://localhost:8888';
}
// 响应拦截器
axios.interceptors.response.use(
  function (response) {
    // 如果返回的状态码为200，说明接口请求成功，可以正常拿到数据
    // 否则的话抛出错误
    if (response.status === 200) {
      return Promise.resolve(response);
    } else {
      return Promise.reject(response);
    }
  },
  // 服务器状态码不是2开头的的情况
  function (error) {
    if (error.response.status) {
      return Promise.reject(error.response);
    }
  });

/**
   * 封装get方法
   * @param url
   * @param data
   * @returns {Promise}
   */

export function get(url, params = {}) {
  let loadingInstance = Loading.service({ fullscreen: true,text:'加载中...',spinner: 'el-icon-loading' });
  return new Promise(function (resolve, reject) {
    axios.get(url, {
      params: params
    })
      .then(function (response) {
        resolve(response.data);
        loadingInstance.close();
      })
      .catch(function (err) {
        reject(err)
        loadingInstance.close();
      })
  })
}


/**
   * 封装post请求
   * @param url
   * @param data
   * @returns {Promise}
   */

export function post(url, data = {}) {
  let loadingInstance = Loading.service({ fullscreen: true,text:'加载中...',spinner: 'el-icon-loading' });
  if(url != "/demo/login/login"){
    return new Promise(function (resolve, reject) {
      axios.post(url, data, {
        headers: {
          'Content-Type': "application/json;charset=UTF-8;multipart/form-data;",
          'Authorization': VueCookie.get('token')
        },
      })
        .then( function (response) {
          resolve(response.data);
          loadingInstance.close();
        }, function (err) {
          reject(err)
          if (err.data.code === 1001) {
            MessageBox.alert(err.data.message, '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning',
              showClose: true
            }).then(function () {
              that.$router.push({ path: '/page/login/login'})
            })
          } else if (err.data.code === 1002) {
            MessageBox.alert(err.data.message, '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning',
              showClose: true
            }).then( function () {
              that.$router.push({ path: '/page/login/login'})
            })
          }
          loadingInstance.close();
        })
    })
  }else{
    return new Promise(function (resolve, reject) {
      axios.post(url, data)
        .then(function (response) {
          resolve(response.data);
          loadingInstance.close();
          VueCookie.set('token', response.data.token, 1);   //设置token,为后台返回的值	
          VueCookie.set('userId', response.data.userId, 1);   //设置token,为后台返回的值
          console.log("token："+ VueCookie.get("token"));
        }, function (err) {
          reject(err)
          loadingInstance.close();
        })
    })
  }
  
}

export function newpost(url, data = {}) {
  let loadingInstance = Loading.service({ fullscreen: true,text:'加载中...',spinner: 'el-icon-loading' });
  if(url != "/demo/login/login"){
    return new Promise(function (resolve, reject) {
      axios.post(url, data, {
        headers: {
          'Authorization': VueCookie.get('token'),
          'ContentType': "application/json;charset=UTF-8;multipart/form-data;"
        },
      })
        .then( function (response) {
          resolve(response.data);
          loadingInstance.close();
        }, function (err) {
          reject(err)
          if (err.data.code === 1001) {
            MessageBox.alert(err.data.message, '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning',
              showClose: true
            }).then(function () {
              that.$router.push({ path: '/page/login/login'})
            })
          } else if (err.data.code === 1002) {
            MessageBox.alert(err.data.message, '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning',
              showClose: true
            }).then( function () {
              that.$router.push({ path: '/page/login/login'})
            })
          }
          loadingInstance.close();
        })
    })
  }else{
    return new Promise(function (resolve, reject) {
      axios.post(url, data)
        .then(function (response) {
          resolve(response.data);
          loadingInstance.close();
          VueCookie.set('token', response.data.token, 1);   //设置token,为后台返回的值	
          VueCookie.set('userId', response.data.userId, 1);   //设置token,为后台返回的值
          console.log("token："+ VueCookie.get("token"));
        }, function (err) {
          reject(err)
          loadingInstance.close();
        })
    })
  }
  
}
/**
   * 封装delete请求
   * @param url
   * @param data
   * @returns {Promise}
   */

export function deletes(url, data = {}) {
  let loadingInstance = Loading.service({ fullscreen: true,text:'加载中...',spinner: 'el-icon-loading' });
  return new Promise(function (resolve, reject) {
    axios.delete(url, data)
      .then(function (response) {
        resolve(response.data);
        loadingInstance.close();
      }, function (err) {
        reject(err)
        loadingInstance.close();
      })
  })
}

/**
   * 封装put请求
   * @param url
   * @param data
   * @returns {Promise}
   */

export function put(url, data = {}) {
  let loadingInstance = Loading.service({ fullscreen: true,text:'加载中...',spinner: 'el-icon-loading' });
  return new Promise(function (resolve, reject) {
    axios.put(url, data)
      .then(function (response) {
        resolve(response.data)
        loadingInstance.close();
      }, function (err) {
        reject(err)
        loadingInstance.close();
      })
  })
}
